local skel = fk.CreateSkill {
  name = "emo__chuyuan",
}

Fk:loadTranslationTable{
  ["emo__chuyuan"] = "除怨",
  [":emo__chuyuan"] = "结束阶段，或你受到伤害后，你可以获得一名角色的一张〖朽域〗牌，且可以对你至其最短路径上的一名角色使用之。",

  ["#emo__chuyuan-choose"] = "除怨：可以获得一名角色的一张〖朽域〗牌",
  ["#emo__chuyuan-use"] = "除怨：你可以对最短路径上的一名角色使用 %arg ",
}

local spec = {
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(player.room.alive_players, function (p)
        return #p:getPile("emo__xiuyu") > 0
      end)
    if #targets == 0 then return false end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__chuyuan-choose",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  ---@param player ServerPlayer
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cid = room:askToChooseCard(player, {
      target = to, skill_name = skel.name,
      flag = { card_data = { { "emo__xiuyu", to:getPile("emo__xiuyu") } } }
    })
    room:obtainCard(player, cid, true, fk.ReasonPrey, player, skel.name)
    if player.dead or not table.contains(player.player_cards[Player.Hand], cid) then return end
    Fk:filterCard(cid, player)
    local card = Fk:getCardById(cid)
    if not player:canUse(card, {bypass_distances = true, bypass_times = true}) or player:prohibitUse(card) then return end
    if card.skill:getMinTargetNum(player) > 1 then return end -- 禁止借刀
    local left, right = {}, {}
    local temp = player
    while true do
      if not temp.dead then
        table.insert(right, temp)
      end
      if temp == to then break end
      temp = temp.next
    end
    while true do
      if not temp.dead then
        table.insert(left, temp)
      end
      if temp == player then break end
      temp = temp.next
    end
    local targets = left
    if #left > #right then
      targets = right
    elseif #left == #right then
      table.insertTableIfNeed(targets, right)
    end
    targets = table.filter(targets, function (p)
      return card.skill:modTargetFilter(player, p, {}, card, {bypass_distances = true, bypass_times = true})
      and not player:isProhibited(p, card)
    end)
    if #targets == 0 then return end
    local tos = room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = targets, skill_name = skel.name,
      prompt = "#emo__chuyuan-use:::" .. card:toLogString(),
    })
    if #tos > 0 then
      room:useCard{
        from = player, tos = tos, card = card, extraUse = true,
      }
    end
  end,
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and player.phase == Player.Finish then
      return table.find(player.room.alive_players, function (p)
        return #p:getPile("emo__xiuyu") > 0
      end) ~= nil
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})


skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return table.find(player.room.alive_players, function (p)
        return #p:getPile("emo__xiuyu") > 0
      end) ~= nil
    end
  end,
  on_cost = spec.on_cost,
  on_use = spec.on_use,
})

return skel
